// components/iframe/comment-send.js
import util from '../../utils/util.js'
import helper from '../../utils/helper.js'
const db = wx.cloud.database()
const app = getApp()
let userInfo = {}
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    modalShow: Boolean,
    loginShow: Boolean,
    blogId: String,
    blog: Object,
    userInfo: Object
  },

  /**
   * 组件的初始数据
   */
  data: {
    content: '',
    loading: false
  },

  observers: {
    userInfo(newval) {
      userInfo = newval
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onLoginsuccess(event) {
      userInfo = event.detail
      // 授权框消失，评论框显示
      this.setData({
        loginShow: false
      }, () => {
        this.setData({
          modalShow: true
        })
      })
    },

    onLoginfail() {
      wx.showModal({
        title: '授权用户才能进行评价',
        content: '',
      })
    },


    subscribe(content) {
      let nickName = wx.getStorageSync('userinfo').nickName
      wx.cloud.callFunction({
        name: 'sendMessage',
        data: {
          nickName,
          content,
          blogId: this.properties.blogId
        }
      }).then((res) => {
        console.log(res)
      })
    },
    onSend: function(event) {
      console.log(event)
      // 插入数据库
      let content = event.detail.value.content
      if (content.trim() == '') {
        wx.showModal({
          title: '评论内容不能为空',
          content: '',
        })
        return
      }
      // wx.requestSubscribeMessage({
      //   tmplIds: [app.globalData.COMMENT],
      //   success: (res) => {
      //     console.log('成功后的回调', res)
      //     // 推送模板消息
      //     if (res[app.globalData.COMMENT] == 'accept') {
      //       this.subscribe(content)
      //     } else if (res[app.globalData.COMMENT] == 'reject') {
      //       wx.showToast({
      //         icon: 'none',
      //         title: '拒绝授权，将无法接受到评论成功提醒'
      //       })
      //     }
      //   },
      //   complete: (res) => {
      //     console.log('res:::', res)

      //   },
      //   fail: (err) => {
      //     console.log(err)
      //   }
      // })
      // 校验评论内容
      util.contentSecCheck(content).then(res => {
        // wx.showLoading({
        //   title: '评论中',
        //   mask: true,
        // })
        this.setData({
          loading: true
        })
        db.collection('blog-comment').add({
          data: {
            content,
            createTime: db.serverDate(),
            blogId: this.properties.blogId,
            nickName: userInfo.nickName,
            avatarUrl: userInfo.avatarUrl
          }
        }).then((res) => {
          // wx.hideLoading()
          console.log('res:::::::::', res)
          wx.showToast({
            title: '评论成功',
          })
          this.setData({
            modalShow: false,
            content: '',
            loading: false
          })

          // 父元素刷新评论页面
          this.triggerEvent('refreshCommentList')
        })
      }).catch(err => {
        this.setData({
          content: '',
          loading: false
        })
      })

      
    }
  }
})
